package com.test.chat;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
/**
* @类名  Myclient.java
* @作者  jinbanglong
* @版本  V1.0
* @日期  2019年6月23日-下午2:17:47
* @描述  
*/
public class Myclient {
    public static void main(String[] args) throws IOException {
        Socket client=null;
        try {
            client=new Socket("localhost", 6666);
        } catch (IOException e) {
            System.err.println("jinbl"+"连接失败");
        }
        new Thread(new Send(client,"jinbl")).start();
//        new Thread(new Recevice(client)).start();
    }
    
}
class Send implements Runnable{
	 Socket client=null;
    private DataOutputStream os;
    private BufferedReader console;
    
    public Send(Socket client,String name) {
        try {
        	this.client=client;
            os=new DataOutputStream(client.getOutputStream());
            console=new BufferedReader(new InputStreamReader(System.in));
//            send(name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
    	while(true) {
    		System.out.println("文件：");
            String msg=new Scanner(System.in).nextLine();
            String[] file = msg.split(","); 
            
            try {
            	for(String path:file) {
            		File f= new File(path);
                	DataInputStream dis = new  DataInputStream(new FileInputStream(path));
                	System.out.println(f.getName());
                	os.writeUTF(f.getName());//文件名
                	os.writeLong(f.length());//文件大小
                	byte[] b =new byte[1024];
                	int len;
                	while((len=dis.read(b))!=-1) {
                		os.write(b, 0, len);
                		os.flush();
                	}
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    	}
    }
    
}
class Recevice implements Runnable{
    
    private DataInputStream is;
    @Override
    public void run() {
        while(true){
            recevice();
        }
    }
    
    public Recevice(Socket client) {
        try {
            is=new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
//            CloseUtil.closeAll(is);
        }
    }  
    
    public void recevice() {
        String msg=null;
        try {
            msg=is.readUTF();
        } catch (IOException e) {
            e.printStackTrace();
//            CloseUtil.closeAll(is);
        }
        System.out.println(msg);
    }
    
}

